/**
 * 
 */
package com.nari.baseinfo.dao;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.nari.baseinfo.model.IQueryer;
import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.rescontroller.dao.IResourceAccessControlDao;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class QueryerDaoImpl extends GenericDaoImpl<IQueryer, String> implements IQueryerDao {
	private IResourceAccessControlDao iResourceAccessControlDao;

	/**
	 * 查询器分页查询
	 * @param queryerName 查询器名称(可选)
	 * @param queryerNo 查询器编号(可选)
	 * @param districtCityID 所属区级机构(可选)
	 * @param capitalCityID 所属市级机构(可选)
	 * @param provinceCityID 所属省级机构(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-27
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IQueryer> queryQueryerPage(String queryerName, String queryerNo, 
			String districtCityID, String capitalCityID, String provinceCityID, 
			PageCond pageCond) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(IQueryer.class);
		Criteria countCriteria = this.getCurrentSession().createCriteria(IQueryer.class);
		//查询器名称
		if(queryerName!=null && !"".equals(queryerName.trim())) {
			listCriteria.add(Restrictions.ilike("queryerName", queryerName.trim().toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("queryerName", queryerName.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		//查询器编号
		if(queryerNo!=null && !"".equals(queryerNo.trim())) {
			listCriteria.add(Restrictions.ilike("queryerNo", queryerNo.trim().toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("queryerNo", queryerNo.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		//所属省级机构
		if(provinceCityID!=null && !"".equals(provinceCityID.trim())) {
			listCriteria.add(Restrictions.eq("provinceCityID", provinceCityID.trim()));
		}
		//所属市级机构
		if(capitalCityID!=null && !"".equals(capitalCityID.trim())) {
			listCriteria.add(Restrictions.eq("capitalCityID", capitalCityID.trim()));
		}
		//所属区级机构
		if(districtCityID!=null && !"".equals(districtCityID.trim())) {
			listCriteria.add(Restrictions.eq("districtCityID", districtCityID.trim()));
		}
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
		if(!"".equals(authorityRes)) {
			//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
			listCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
			countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
		}
		//统计结果集个数
		int total = ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
		//结果集排序
		listCriteria.addOrder(Order.asc("queryerName")).addOrder(Order.asc("queryerNo"));
		//结果集分页
		listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IQueryer> queryerPage = new ResultWithPageCond<IQueryer>();
		queryerPage.setList(listCriteria.list());
		queryerPage.setPageCond(pageCond);
		return queryerPage;
	}
	
	/**
	 * 统计相同查询器编号的查询器数量，避免系统录入相同查询器编号的查询器信息
	 * @param queryerNo
	 * @param queryerUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-7-24
	 */
	public int countByQueryerNo(String queryerNo, String queryerUID) throws Exception {
		String hsql = "select count(*) from IQueryer q " +
				"where q.queryerNo='"+queryerNo+"'";
		if(queryerUID!=null && !"".equals(queryerUID)) {
			hsql += " and q.id<>'"+queryerUID+"'";
		}
		return ((Long)this.getCurrentSession().createQuery(hsql).uniqueResult()).intValue();
	}

	/**
	 * @return the iResourceAccessControlDao
	 */
	public IResourceAccessControlDao getiResourceAccessControlDao() {
		return iResourceAccessControlDao;
	}

	/**
	 * @param iResourceAccessControlDao the iResourceAccessControlDao to set
	 */
	public void setiResourceAccessControlDao(
			IResourceAccessControlDao iResourceAccessControlDao) {
		this.iResourceAccessControlDao = iResourceAccessControlDao;
	}
	
}
